 var $imgindex = 0;
// Before using, must create <div id="dialog-confirm"></div>
$(document).ready(function(){
    $('#dialog-confirm').hide();
});
function jqueryui_confirm(height, url) {
    if (typeof height === 'undefined') height = 200;
    var result;
    $("#dialog-confirm").dialog({
        resizable: false,
        height: height,
        modal: true,
        buttons: {
            "OK": function() {
                $( this ).dialog( "close" );
                window.location = url;
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }
    });
}
 function changeImage($index){
    $('.arrow-rightsld').removeClass('arrow-rightsld');
    $('.arrow-right').eq($index).addClass('arrow-rightsld');
 }
 function updateUrl($url) {
    // alert($('#hselected_img').parent());
    $('#hselected_img').parent().attr('href', $url);
 }
 function updateImage($new) {
    var options = {};
    var selectedEffect = 'fade';
    if (undefined == this.current) this.current = 0;
    else this.current = (this.current + 1) % 7;
    switch (this.current) {
        case 6:
            selectedEffect = 'puff';
            break;
        case 1:
            selectedEffect = 'scale';
            options = { percent: 25 };
            break;
        case 2:
            selectedEffect = 'bounce';
            options = { distance: 250, times: 1, direction: 'left' };
            break;
        case 3:
            selectedEffect = 'fade';
            break;
        case 4:
            selectedEffect = 'explode';
            options = { pieces: 9 };
            break;
        case 5:
            selectedEffect = 'explode';
            options = { pieces: 16 };
            break;
        case 0:
            selectedEffect = 'bounce';
            options = { distance: 50, times: 1 };
            break;
    }
    $('#hselected').hide(selectedEffect, options, 700, function() {
        $('#hselected_img').attr('src', $new);
        $('#hselected').fadeIn();
    });
 }
 var justChanged = false;
 function load($time){
    setInterval(function(){
        if (justChanged) {
            justChanged = false;
            return ;
        }
        $imgindex ++;
        if($imgindex > 3){
            $imgindex = 0;
        }
        changeImage($imgindex);
        $bg = $('.hlimg').eq($imgindex).attr('src');
        $new = $bg;
        updateImage($new);
    },$time);
 }
 $(document).ready(function(){   
    load(6000);
    $('.hitems').mouseout(function(){
        $index = $(this).index('.hitems');
        $('.arrow-right').eq($index).removeClass('arrow-righthv');
    });
    
    $('.hitems').mouseover(function(){
        $index = $(this).index('.hitems');
    });
       
    $('.hitems').click(function(){
        justChanged = true;
        $index = $(this).index('.hitems');
        changeImage($index);
        $bg = $('.hlimg').eq($index).attr('src');
        $new = $bg;
        updateImage($new);
        updateUrl($('.hlimg').eq($index).attr('link'));
        $imgindex = $index;
    });

    $('#view_most_viewed').click(function() {
        $.ajax({
            type: 'POST',
            url: webroot + 'products/ajaxSearchOption/popular',
            success: function() {
                window.location = 'products/search';
            }
        });
    });
    $('#view_best_rated').click(function() {
        $.ajax({
            type: 'POST',
            url: webroot + 'products/ajaxSearchOption/rating',
            success: function() {
                window.location = 'products/search';
            }
        });
    });
 });
